package TestDemo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestDemo7 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable callable = new Callable() {
            @Override
            public Integer call() throws Exception {
                int n = 10;
                return n;
            }
        };
        // 不能直接放进Thread中, 需要包装一层来获取返回值
        FutureTask<Integer> task = new FutureTask<>(callable);
        Thread t = new Thread(task);
        t.start();

        // 在线程t没有执行完之前, get会阻塞.
        System.out.println(task.get());
    }
}
